/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.sanerzone.smscenter.modules.sms.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sanerzone.jeebase.common.config.Global;
import com.sanerzone.jeebase.common.persistence.Page;
import com.sanerzone.jeebase.common.utils.StringUtils;
import com.sanerzone.jeebase.common.web.BaseController;
import com.sanerzone.jeebase.modules.sys.entity.User;
import com.sanerzone.jeebase.modules.sys.utils.UserUtils;
import com.sanerzone.smscenter.modules.sms.entity.SmsSubmitTemplate;
import com.sanerzone.smscenter.modules.sms.service.SmsSubmitTemplateService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Date;


/**
 * 单表生成Controller
 * @author ThinkGem
 * @version 2017-12-25
 */
@Controller
@RequestMapping(value = "${adminPath}/sms/smsSubmitTemplate")
public class SmsSubmitTemplateController extends BaseController {

	@Autowired
	private SmsSubmitTemplateService smsSubmitTemplateService;
	
	@ModelAttribute
	public SmsSubmitTemplate get(@RequestParam(required=false) String id) {
		SmsSubmitTemplate entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = smsSubmitTemplateService.get(id);
		}
		if (entity == null){
			entity = new SmsSubmitTemplate();
		}
		return entity;
	}
	
	@RequiresPermissions("sms:smsSubmitTemplate:view")
	@RequestMapping(value = {"list", ""})
	public String list(SmsSubmitTemplate smsSubmitTemplate, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<SmsSubmitTemplate> page = smsSubmitTemplateService.findPage(new Page<SmsSubmitTemplate>(request, response), smsSubmitTemplate);
		model.addAttribute("page", page);
		return "modules/sms/smsSubmitTemplateList";
	}

	@RequiresPermissions("sms:smsSubmitTemplate:view")
	@RequestMapping(value = "form")
	public String form(SmsSubmitTemplate smsSubmitTemplate, Model model) {
		model.addAttribute("smsSubmitTemplate", smsSubmitTemplate);
		return "modules/sms/smsSubmitTemplateForm";
	}

	@RequiresPermissions("sms:smsSubmitTemplate:edit")
	@RequestMapping(value = "save")
	public String save(SmsSubmitTemplate smsSubmitTemplate, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, smsSubmitTemplate)){
			return form(smsSubmitTemplate, model);
		}
		User user =  UserUtils.getUser();
		if (user != null) {
			smsSubmitTemplate.setCreateBy(user);
			smsSubmitTemplate.setRecheckUserId(user.getId());
		}
		smsSubmitTemplateService.save(smsSubmitTemplate);
		addMessage(redirectAttributes, "保存单表成功");
		return "redirect:"+ Global.getAdminPath()+"/sms/smsSubmitTemplate/?repage";
	}
	
	@RequiresPermissions("sms:smsSubmitTemplate:edit")
	@RequestMapping(value = "delete")
	public String delete(SmsSubmitTemplate smsSubmitTemplate, RedirectAttributes redirectAttributes) {
		smsSubmitTemplateService.delete(smsSubmitTemplate);
		addMessage(redirectAttributes, "删除单表成功");
		return "redirect:"+Global.getAdminPath()+"/sms/smsSubmitTemplate/?repage";
	}

}